<template>
	<xn-form-container
		:title="formData.id ? '编辑职工议案' : '增加职工议案'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="选择职代会" name="congressId">
				<a-select
					v-model:value="formData.congressId"
					label-in-value
					placeholder="请输入对应的职代会名称（支持模糊查找）"
					style="width: 100%"
					:filter-option="false"
					:options="congressData"
					@search="fetchCongress"
				>
					<template v-if="fetching" #notFoundContent>
						<a-spin size="small" />
					</template>
				</a-select>
			</a-form-item>
			<a-form-item label="上传议案文件" name="motionFileId">
				<div>
					<a-upload
						list-type="picture"
						v-model:file-list="fileList"
						:remove="handleRemove"
						:before-upload="beforeUpload"
						class="upload-list-inline"
					>
						<a-button>
							<upload-outlined></upload-outlined>
							选择上传的议案文件
						</a-button>
					</a-upload>
				</div>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="bizMotionForm">
import { cloneDeep, debounce } from 'lodash-es'
import { required } from '@/utils/formRules'
import bizMotionApi from '@/api/biz/bizMotionApi'
import bizCongressApi from '@/api/biz/bizCongressApi'
import fileApi from '@/api/biz/bizFileManagerApi.js'
// 抽屉状态
const open = ref(false)
const emit = defineEmits({ successful: null })
const formRef = ref()
const fileList = ref([])
// 表单数据
const formData = ref({})
const submitLoading = ref(false)
// 搜索数据
const congressData = ref([])

onMounted(() => {
	bizCongressApi.bizCongressList().then((res) => {
		let list = []
		res.forEach((element) => {
			list.push({
				key: element.id,
				title: element.congressName,
				value: element.congressName
			})
		})
		congressData.value = list
	})
})

// 打开抽屉
const onOpen = (record) => {
	open.value = true
	if (record) {
		let recordData = cloneDeep(record)
		formData.value = Object.assign({}, recordData)
	}
}
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	formData.value = {}
	open.value = false
}
// 默认要校验的
const formRules = {}

// 文件处理（等选择后手动上传）
const handleRemove = (file) => {
	const index = fileList.value.indexOf(file)
	const newFileList = fileList.value.slice()
	newFileList.splice(index, 1)
	fileList.value = newFileList
	console.log(newFileList)
}

const beforeUpload = (file) => {
	fileList.value = [...fileList.value, file]
	return false
}

// 职代会搜索框(1秒以后查)
const fetchCongress = debounce((value) => {
	console.log('搜索职代会' + value)
	bizCongressApi
		.bizCongressListByCon({
			congressName: value
		})
		.then((res) => {
			console.log('搜索职代会结果' + res)
		})
}, 400)

// 文件上传
const uploadFile = async () => {
	const formData = new FormData()
	fileList.value.forEach((file) => {
		file = toRaw(file)
		formData.append('files', file.originFileObj)
	})
	return fileApi.fileUploadLocalReturnIdList(formData)
}

// 验证并提交数据
const onSubmit = async () => {
	let res = await uploadFile()
	submitLoading.value = true
	const formDataParam = cloneDeep(formData.value)
	console.log(formDataParam)
	formDataParam.congressName = formDataParam.congressId.option.value
	formDataParam.congressId = formDataParam.congressId.option.key
	formDataParam.fileList = res
	console.log(formDataParam)
	bizMotionApi
		.bizMotionSubmitForm(formDataParam, formDataParam.id)
		.then(() => {
			onClose()
			emit('successful')
		})
		.finally(() => {
			submitLoading.value = false
		})
}
//

// 抛出函数
defineExpose({
	onOpen
})
</script>
<style>
/* tile uploaded pictures */
.upload-list-inline :deep(.ant-upload-list-item) {
	float: left;
	width: 200px;
	margin-right: 8px;
}
.upload-list-inline :deep(.ant-upload-animate-enter) {
	animation-name: uploadAnimateInlineIn;
}
.upload-list-inline :deep(.ant-upload-animate-leave) {
	animation-name: uploadAnimateInlineOut;
}
</style>
